package com.anlogic.sdk.bsp.settings;

import java.util.ArrayList;
import java.util.Iterator;
import java.util.List;

public class DriverAssignment {
	private String hwInstanceName = "";

	private String hwInstanceType = "";

	private List<Component> drivers = new ArrayList<Component>();

	private int selectedDriverIndex = -1;

	public void addDriver(String name, String ver) {
		Component c = findDriver(name);
		if (c == null) {
			c = new Component();
			c.setName(name);
			this.drivers.add(c);
		}
		c.addVersion(ver);
	}

	public void addDriver(String name, String[] versions) {
		Component c = findDriver(name);
		if (c == null) {
			c = new Component();
			c.setName(name);
			c.addVersions(versions);
			this.drivers.add(c);
		}
	}

	public void setSelectedDriver(String name, String ver) {
		Component c = findDriver(name);
		if (c == null) {
			c = new Component();
			c.setName(name);
			c.addVersion(ver);
			this.drivers.add(c);
		}
		this.selectedDriverIndex = this.drivers.indexOf(c);
		if (c.getVersions().indexOf(ver) == -1)
			c.addVersion(ver);
		c.setSelectedVersion(ver);
	}

	public Component getSelectedDriver() {
		if (this.selectedDriverIndex == -1)
			return null;
		return this.drivers.get(this.selectedDriverIndex);
	}

	public int getSelectedDriverIndex() {
		return this.selectedDriverIndex;
	}

	public void setSelectedDriverIndex(int selectedDriverIndex) {
		this.selectedDriverIndex = selectedDriverIndex;
	}

	public String getHwInstanceName() {
		return this.hwInstanceName;
	}

	public void setHwInstanceName(String hwInstanceName) {
		this.hwInstanceName = hwInstanceName;
	}

	public String getHwInstanceType() {
		return this.hwInstanceType;
	}

	public void setHwInstanceType(String hwInstanceType) {
		this.hwInstanceType = hwInstanceType;
	}

	public List<Component> getDrivers() {
		return this.drivers;
	}

	public void setDrivers(List<Component> drivers) {
		this.drivers = drivers;
	}

	public String[] getDriverNames() {
		String[] driverNames = new String[this.drivers.size()];
		for (int i = 0; i < this.drivers.size(); i++)
			driverNames[i] = ((Component) this.drivers.get(i)).getName();
		return driverNames;
	}

	private Component findDriver(String name) {
		Iterator<Component> itr = this.drivers.iterator();
		while (itr.hasNext()) {
			Component c = itr.next();
			if (c.getName().equals(name))
				return c;
		}
		return null;
	}
}
